<template>
  <div class="mod-user">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.name" placeholder="产品ID" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button v-if="isAuth('sys:product:save')" type="primary" @click="addProductHandle()">新增</el-button>
        <!-- <el-button v-if="isAuth('sys:user:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button> -->
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      row-key="id"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="productId"
        header-align="center"
        align="center"
        width="200"
        label="ID">
      </el-table-column>
      <el-table-column
        prop="productName"
        header-align="center"
        align="center"
        width="200"
        label="产品名称">
      </el-table-column>

      <el-table-column
        prop="interestRate"
        header-align="center"
        align="center"
        width="100"
        label="年化">
        <template slot-scope="scope">
          {{scope.row.interestRate }} %
        </template>
      </el-table-column>
      <el-table-column
        prop="depositTime"
        header-align="center"
        align="center"
        width="120"
        label="存款时间">
        <template slot-scope="scope">
          {{scope.row.depositTime | formatDepositTime}}
        </template>
      </el-table-column>
      <el-table-column
        prop="riskLevel"
        header-align="center"
        align="center"
        width="150"
        label="风险等级">
        <template slot-scope="scope">
          <span v-if="scope.row.riskLevel == 1" >低风险</span>
          <span v-else-if="scope.row.riskLevel == 2" >中风险</span>
          <span v-else >高风险</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="cashAdvance"
        header-align="center"
        align="center"
        width="160"
        label="是否可以提前提取">
        <template slot-scope="scope">
          {{scope.row.cashAdvance ? '是' : '否'}}
        </template>
      </el-table-column>
      <el-table-column
        prop="autoRedemption"
        header-align="center"
        align="center"
        width="160"
        label="是否可以自动赎回">
        <template slot-scope="scope">
          {{scope.row.autoRedemption ? '是' : '否'}}
        </template>
      </el-table-column>
      <!-- <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" size="small" type="danger">禁用</el-tag>
          <el-tag v-else size="small">正常</el-tag>
        </template>
      </el-table-column> -->
      <el-table-column
        prop="gmtCreate"
        header-align="center"
        align="center"
        width="180"
        label="创建时间">
      </el-table-column>
      <el-table-column
        prop="gmtModified"
        header-align="center"
        align="center"
        width="180"
        label="修改时间">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button v-if="isAuth('sys:product:update')" type="text" size="small" @click="addProductHandle(scope.row.productId)">修改</el-button>
          <!-- <el-button v-if="isAuth('sys:user:delete')" type="text" size="small" @click="deleteHandle(scope.row.productId, scope.row.productName)">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 秒杀产品：新增 / 修改 -->
    <add-product v-if="addProductVisible" ref="addProduct" @refreshDataList="getDataList"></add-product>

  </div>
</template>

<script>
  import addProduct from './add.vue'
  export default {
    data () {
      return {
        dataForm: {
          name: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        addProductVisible: false
      }
    },
    components: {
      addProduct
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/coupon/coupon/product/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 秒杀活动：新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 秒杀产品：新增 / 修改
      addProductHandle (id) {
        this.addProductVisible = true
        this.$nextTick(() => {
          this.$refs.addProduct.init(id)
        })
      },
      // 删除
      deleteHandle (id, productName) {
        var productNames = productName ? [productName] : this.dataListSelections.map(item => {
          return item.productName
        })
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.productId
        })
        this.$confirm(`确定对[产品名称=${productNames.join(',')}]进行[${productNames ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/coupon/coupon/product/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }).catch(() => {})
      }
    },
    filters: {
      formatDepositTime (v) {
        let arr = v && v.split('-')
        console.log(arr)
        return arr[0] > 0 ? arr[0] + '年' : '' + arr[1] > 0 ? arr[1] + '月' : ''
      }
    }
  }
</script>
